package com.ruoyi.system.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ruoyi.common.event.MyCustomEvent;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.system.domain.EmProperties;
import com.ruoyi.system.domain.EmPropertiesHistory;
import com.ruoyi.system.service.IEmPropertiesHistoryService;
import com.ruoyi.system.service.IEmPropertiesService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class MyCustomEventListener implements ApplicationListener<MyCustomEvent> {
    private final IEmPropertiesService propertiesService;
    private final IEmPropertiesHistoryService historyService;
    private final ObjectMapper objectMapper;

    @SneakyThrows
    @Override
    public void onApplicationEvent(MyCustomEvent event) {
        System.out.println("Received event: " + event.getMessage());
        String message = event.getMessage();
        ObjectNode jsonNodes = objectMapper.readValue(message, ObjectNode.class);
        EmPropertiesHistory entity = new EmPropertiesHistory();
        entity.setEmCode(JsonUtils.getStr(jsonNodes, "field1"));
        entity.setEmName(JsonUtils.getStr(jsonNodes, "field2"));
        entity.setEmType(JsonUtils.getStr(jsonNodes, "field3"));
        entity.setEmStatus(JsonUtils.getStr(jsonNodes, "field4"));
        entity.setEmCompany(JsonUtils.getStr(jsonNodes, "field5"));
        entity.setEmLocation(JsonUtils.getStr(jsonNodes, "field6"));
        entity.setEmArea(JsonUtils.getLong(jsonNodes, "field7"));
        entity.setEmOwnershipS(JsonUtils.getStr(jsonNodes, "field8"));
        entity.setEmOwnershipName(JsonUtils.getStr(jsonNodes, "field9"));
        entity.setEmOwnershipMix(JsonUtils.getStr(jsonNodes, "field10"));
        entity.setEmMortgageY(JsonUtils.getStr(jsonNodes, "field11"));
        entity.setEmMortgageName(JsonUtils.getStr(jsonNodes, "field12"));
        entity.setEmCertificateN(JsonUtils.getStr(jsonNodes, "field13"));
        entity.setEmCertificateF(JsonUtils.getStr(jsonNodes, "field14"));
        entity.setEmCertificateT(JsonUtils.getStr(jsonNodes, "field15"));
        entity.setEmLandnature(JsonUtils.getStr(jsonNodes, "field16"));
        entity.setEmHoldingtime(JsonUtils.getDate(jsonNodes, "field17"));
        entity.setEmHoldingway(JsonUtils.getStr(jsonNodes, "field18"));
        entity.setEmTao(JsonUtils.getLong(jsonNodes, "field19"));
        entity.setEmFinAccount(JsonUtils.getStr(jsonNodes, "field20"));
        entity.setEmFinBook(JsonUtils.getLong(jsonNodes, "field21"));
        entity.setEmFinObook(JsonUtils.getLong(jsonNodes, "field22"));
        entity.setEmEval(JsonUtils.getStr(jsonNodes, "field23"));
        entity.setEmEvalPrice(JsonUtils.getLong(jsonNodes, "field24"));
        entity.setEmEvalTime(JsonUtils.getDate(jsonNodes, "field25"));
        entity.setEmEvalAgency(JsonUtils.getStr(jsonNodes, "field26"));
        entity.setEmInPaper(JsonUtils.getStr(jsonNodes, "field27"));
        entity.setEmInBc1(JsonUtils.getStr(jsonNodes, "field28"));
        entity.setEmInBc2(JsonUtils.getStr(jsonNodes, "field29"));
        entity.setEmInBc3(JsonUtils.getStr(jsonNodes, "field30"));
        entity.setEmInLabel(JsonUtils.getStr(jsonNodes, "field31"));
        entity.setEmInFlocation(JsonUtils.getStr(jsonNodes, "field32"));
        entity.setEmMonthIdle(JsonUtils.getLong(jsonNodes, "field33"));
        entity.setEmMonthIdleR(JsonUtils.getStr(jsonNodes, "field34"));
        entity.setEmMonthUse(JsonUtils.getLong(jsonNodes, "field35"));
        entity.setEmOwnWay(JsonUtils.getStr(jsonNodes, "field36"));
        entity.setEmMerchantsWay(JsonUtils.getStr(jsonNodes, "field37"));
        entity.setEmMerchantsPlm(JsonUtils.getStr(jsonNodes, "field38"));
        entity.setEmMerchantsUnitw(JsonUtils.getStr(jsonNodes, "field39"));
        entity.setEmMerchantsDj(JsonUtils.getLong(jsonNodes, "field40"));
        entity.setEmMeexcelPaper(JsonUtils.getStr(jsonNodes, "field41"));
        entity.setEmMeexcelBc1(JsonUtils.getStr(jsonNodes, "field42"));
        entity.setEmMeexcelBc2(JsonUtils.getStr(jsonNodes, "field43"));
        entity.setEmMeexcelBc3(JsonUtils.getStr(jsonNodes, "field44"));
        entity.setEmMeexcelLabel(JsonUtils.getStr(jsonNodes, "field45"));
        entity.setEmMeexcelFlocation(JsonUtils.getStr(jsonNodes, "field46"));
        entity.setEmMerchantsNumb(JsonUtils.getLong(jsonNodes, "field47"));
        entity.setEmMerchantsUnitp(JsonUtils.getLong(jsonNodes, "field48"));
        entity.setEmMerchantsTotal(JsonUtils.getLong(jsonNodes, "field49"));
        entity.setEmMerchantsObject(JsonUtils.getStr(jsonNodes, "field50"));
        entity.setEmMerchantsReal(JsonUtils.getStr(jsonNodes, "field51"));
        entity.setEmMerchantsYn(JsonUtils.getStr(jsonNodes, "field52"));
        entity.setEmMerchantsHistory(JsonUtils.getStr(jsonNodes, "field53"));
        entity.setEmMerchantsYears(JsonUtils.getLong(jsonNodes, "field54"));
        entity.setEmMerchantsBegin(JsonUtils.getDate(jsonNodes, "field55"));
        entity.setEmMerchantsEnd(JsonUtils.getDate(jsonNodes, "field56"));
        entity.setEmMerchantsCollectionway(JsonUtils.getStr(jsonNodes, "field57"));
        entity.setEmMerchantsPayway(JsonUtils.getStr(jsonNodes, "field58"));
        entity.setEmDepossit(JsonUtils.getLong(jsonNodes, "field59"));
        entity.setEmAgexcelPaper(JsonUtils.getStr(jsonNodes, "field60"));
        entity.setEmAgexcelBc1(JsonUtils.getStr(jsonNodes, "field61"));
        entity.setEmAgexcelBc2(JsonUtils.getStr(jsonNodes, "field62"));
        entity.setEmAgexcelBc3(JsonUtils.getStr(jsonNodes, "field63"));
        entity.setEmAgexcelLabel(JsonUtils.getStr(jsonNodes, "field64"));
        entity.setEmAgexcelFlocation(JsonUtils.getStr(jsonNodes, "field65"));
        entity.setEmOweTotal(JsonUtils.getLong(jsonNodes, "field66"));
        entity.setEmLiquWay(JsonUtils.getStr(jsonNodes, "field67"));
        entity.setEmRentAble(JsonUtils.getLong(jsonNodes, "field68"));
        entity.setEmRentPaid(JsonUtils.getLong(jsonNodes, "field69"));
        entity.setEmLateStart(JsonUtils.getDate(jsonNodes, "field70"));
        entity.setEmLateAllday(JsonUtils.getLong(jsonNodes, "field71"));
        entity.setEmLateMoney(JsonUtils.getLong(jsonNodes, "field72"));
        entity.setEmLateForpay(JsonUtils.getLong(jsonNodes, "field73"));
        entity.setEmOweLong(JsonUtils.getLong(jsonNodes, "field74"));
        entity.setEmRenexcelPaper(JsonUtils.getStr(jsonNodes, "field75"));
        entity.setEmRenexcelBc1(JsonUtils.getStr(jsonNodes, "field76"));
        entity.setEmRenexcelBc2(JsonUtils.getStr(jsonNodes, "field77"));
        entity.setEmRenexcelBc3(JsonUtils.getStr(jsonNodes, "field78"));
        entity.setEmRenexcelLabel(JsonUtils.getStr(jsonNodes, "field79"));
        entity.setEmRenexcelFlocation(JsonUtils.getStr(jsonNodes, "field80"));
        entity.setEmAgreementNo(JsonUtils.getStr(jsonNodes, "field81"));
        entity.setEmAgreementPrice(JsonUtils.getLong(jsonNodes, "field82"));

        entity.setCreateDept(JsonUtils.getLong(jsonNodes, "createDept"));
        entity.setFormDataType(JsonUtils.getLong(jsonNodes, "formDataType"));
        entity.setRecordCreateTime(JsonUtils.getDate(jsonNodes, "createTime"));
        entity.setStartUserId(JsonUtils.getLong(jsonNodes, "createBy"));
        entity.setProcessInstanceId(JsonUtils.getLong(jsonNodes, "processInstanceId"));



        //调用所有的set方法
        historyService.save(entity);

        //更新实时数据
        EmProperties emProperties = propertiesService.lambdaQuery().eq(EmProperties::getEmCode, entity.getEmCode())
            .one();
        EmProperties saveData = new EmProperties();
        BeanUtil.copyProperties(entity, saveData);

        if (ObjUtil.isNull(emProperties)) {
            propertiesService.save(saveData);
        } else {
            //更新
            saveData.setEmId(emProperties.getEmId());
            saveData.setCreateTime(null);
            saveData.setCreateBy(null);
            propertiesService.updateById(saveData);
        }
    }

}
